package org.example.chat.service.impl;

import org.example.chat.aop.annotation.AccessLimiter;
import org.example.chat.aop.annotation.Log;
import org.example.chat.api.Constant;
import org.example.chat.dao.RegionInfoRepository;
import org.example.chat.entity.RegionInfo;
import org.example.chat.service.IRegionInfoService;
import org.example.chat.utils.CommonAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegionInfoServiceImpl extends BaseService implements IRegionInfoService {

    @Autowired
    private RegionInfoRepository regionInfoRepository;

    @Override
    public List<RegionInfo> getRegionInfoBySuperiorCode(String code) {
        String key = Constant.CACHE_DATA_PREFIX + "region_info:" + code;
        List<RegionInfo> regionInfos = commonAlgorithm.mutexQueryCache(key, RegionInfo.class, param -> {
            return regionInfoRepository.findBySuperiorCode(param);
        }, code);
        return regionInfos;
    }

    @AccessLimiter(rate = 50)
    @Log
    @Override
    public List<RegionInfo> queryRegionInfoByName(String name) {
        String key = Constant.CACHE_DATA_PREFIX + "region_info:" + name;
        List<RegionInfo> regionInfos = commonAlgorithm.logicalExpirationQuery(key, RegionInfo.class, param -> {
            return regionInfoRepository.findByParentName(param);
        }, name);
        return regionInfos;
    }
}
